package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.CalcinationPurifyWetElectricity;
import com.hkts.naturalgas.base.entity.HktsTailGas;
import com.hkts.naturalgas.base.entity.HktsWasteGas;
import com.hkts.naturalgas.base.entity.Ozone;
import com.hkts.naturalgas.base.mapper.HktsWasteGasMapper;
import com.hkts.naturalgas.base.service.IHktsWasteGasService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class HktsWasteGasServiceImpl extends ServiceImpl<HktsWasteGasMapper, HktsWasteGas> implements IHktsWasteGasService {


    @Autowired
    private HktsWasteGasMapper hktsWasteGasMapper;

    @Override
    public IPage<HktsWasteGas> selectPage(Page<HktsWasteGas> page, HktsWasteGas hktsWasteGas){
            QueryWrapper<HktsWasteGas> query=Wrappers.query();
            QueryWrapper<HktsWasteGas> entityWrapper=getEntityWrapper(query, hktsWasteGas);
            return hktsWasteGasMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<HktsWasteGas> selectList(HktsWasteGas hktsWasteGas){
            QueryWrapper<HktsWasteGas> query=Wrappers.query();
            QueryWrapper<HktsWasteGas> entityWrapper=getEntityWrapper(query, hktsWasteGas);
            return hktsWasteGasMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsWasteGas> getEntityWrapper(QueryWrapper<HktsWasteGas> entityWrapper,HktsWasteGas hktsWasteGas){
            //条件拼接
            return entityWrapper;
    }
    @Override
    public IPage<HktsWasteGas> selectPageByTime(Page<HktsWasteGas> page, String startDate, String endDate){
        QueryWrapper<HktsWasteGas> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(HktsWasteGas::getTime);
        IPage<HktsWasteGas> hktsTailGasPage = hktsWasteGasMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<HktsWasteGas> getBrokenLineGraph(Page<HktsWasteGas> page, String startDate, String endDate){
        QueryWrapper<HktsWasteGas> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(HktsWasteGas::getTime);
        IPage calcinationDesulfurizationDenitrations = hktsWasteGasMapper.selectPage(page,query);
        List<HktsWasteGas> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(HktsWasteGas vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("废气排放量(m³)",vo.getA00000Cou());
        data.put("氧含量均值(%)",vo.getA19001Avg());
        data.put("氮氧化物实测浓度(mg/m³)",vo.getA21002Avg());
        data.put("二氧化硫实测浓度(mg/m³)",vo.getA21026Avg());
        data.put("烟尘实测浓度(mg/m³)",vo.getA34013Avg());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void importWasteGas(HktsWasteGas gas) {
        List<HktsWasteGas> list=new ArrayList<>(60);
        for(int i=0;i<60;i++){
            HktsWasteGas gas1=new HktsWasteGas();
            gas1.setTime(DataHandlerUtils.dateHandler(gas.getTime(),i));
            gas1.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()) );
            gas1.setStopFlag(gas.getStopFlag());
            gas1.setA34013Avg(DataHandlerUtils.dataHandler(gas.getA34013Avg()));
            gas1.setA21002Avg(DataHandlerUtils.dataHandler(gas.getA21002Avg()));
            gas1.setA19001Avg(DataHandlerUtils.dataHandler(gas.getA19001Avg()));
            gas1.setA00000Cou(DataHandlerUtils.dataHandler(gas.getA00000Cou()));
            gas1.setA21026Avg(DataHandlerUtils.dataHandler(gas.getA21026Avg()));
            list.add(gas1);
        }
        this.saveBatch(list);
    }

    @Override
    public void importOnTimeWasteGas(HktsWasteGas gas) {

            HktsWasteGas gas1=new HktsWasteGas();
            gas1.setTime(gas.getTime());
            gas1.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()) );
            gas1.setStopFlag(gas.getStopFlag());
            gas1.setA34013Avg(DataHandlerUtils.dataHandler(gas.getA34013Avg()));
            gas1.setA21002Avg(DataHandlerUtils.dataHandler(gas.getA21002Avg()));
            gas1.setA19001Avg(DataHandlerUtils.dataHandler(gas.getA19001Avg()));
            gas1.setA00000Cou(DataHandlerUtils.dataHandler(gas.getA00000Cou()));
            gas1.setA21026Avg(DataHandlerUtils.dataHandler(gas.getA21026Avg()));
            hktsWasteGasMapper.insert(gas1);
    }
}
